package com.hdaccp.ch12

import java.sql.{Connection, PreparedStatement}

import com.hdaccp.ch11.{MyLogCourseType, MySQLUtils}

import scala.collection.mutable.ListBuffer

object CoursetypetimesDao {
  def insertCoursetypetimes(list:ListBuffer[Coursetypetimes]): Unit = {
    var connection: Connection = null
    var pstmt: PreparedStatement = null
    try {
      connection = MySQLUtils.getConnection()
      connection.setAutoCommit(false) //设置手动提交
      val sql = "insert into coursetypetimes(coursetype,times) values (?,?) "
      pstmt = connection.prepareStatement(sql)
      for (ele <- list) {
        pstmt.setString(1, ele.coursetype)
        pstmt.setLong(2, ele.times)
        pstmt.addBatch()
      }
      pstmt.executeBatch() // 执行批量处理
      connection.commit() //手工提交
    } catch {
      case e: Exception => e.printStackTrace()
    } finally {
      MySQLUtils.release(connection, pstmt)
    }
  }
}
